package com.mrdeer.config.rabbitmq;

import com.mrdeer.iot.bean.enums.EMQExchange;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 使用广播的方式进行消息传递
 * 1.定义交换机FanoutExchange
 * 2.定义队列Queue
 * 3.将交换机和队列进行绑定
 * @author xie jianchu
 */
@Slf4j
@Configuration
public class RabbitMQConfig {

    @Bean
    FanoutExchange statusFanoutExchange() {
        return new FanoutExchange(EMQExchange.RTU_STATUS_EXCHANGE.getMqFanoutExchange());
    }

    @Bean
    FanoutExchange heartFanoutExchange() {
        return new FanoutExchange(EMQExchange.RTU_HEART_EXCHANGE.getMqFanoutExchange());
    }

    @Bean
    FanoutExchange invalidFanoutExchange() {
        return new FanoutExchange(EMQExchange.RTU_INVALID_EXCHANGE.getMqFanoutExchange());
    }

    @Bean
    FanoutExchange controlFanoutExchange() {
        return new FanoutExchange(EMQExchange.RTU_CONTROL_EXCHANGE.getMqFanoutExchange());
    }

    @Bean
    public Queue instQueue() {
        return new Queue("rtu_inst_queue", true);
    }

    @Bean
    public Binding bindingStatusFanoutExchange(Queue instQueue, FanoutExchange statusFanoutExchange) {
        return BindingBuilder.bind(instQueue).to(statusFanoutExchange);
    }

    @Bean
    public Binding bindingHeartFanoutExchange(Queue instQueue, FanoutExchange heartFanoutExchange) {
        return BindingBuilder.bind(instQueue).to(heartFanoutExchange);
    }

    @Bean
    public Binding bindingInvalidFanoutExchange(Queue instQueue, FanoutExchange invalidFanoutExchange) {
        return BindingBuilder.bind(instQueue).to(invalidFanoutExchange);
    }

    @Bean
    public Binding bindingControlFanoutExchange(Queue instQueue, FanoutExchange controlFanoutExchange) {
        return BindingBuilder.bind(instQueue).to(controlFanoutExchange);
    }

    @Bean
    public Queue refreshQueue() {
        return new Queue("rtu_refresh_queue", true);
    }

}
